package com.enlorenz.core.channel.socket.common.translate;

import java.io.UnsupportedEncodingException;

import net.sf.json.JSONObject;
/**
 * Json消息序列化
 * 使用Json格式对数据进行分解组装
 * @author lcb
 */
public class JsonMessageSerializeTransformer implements IMessageBytesTransformer<Object>{
	//字符编码
	private String encoding="utf-8";
	@Override
	public Object bytes2Message(byte[] data) {
		try {
			String msg=new String(data,encoding);
			JSONObject object = 
					JSONObject.fromObject(msg);
			return object;
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public byte[] message2Bytes(Object message) {
		try {
			return JSONObject.fromObject(message).toString().getBytes(encoding);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
}
